package storage

import "fmt"

// 适配器，
var adapters = make(map[string]Storage)

// Register 注册
func Register(name string, adapter Storage) {
	if adapter == nil {
		panic("storage: Register adapter is nil")
	}
	if _, ok := adapters[name]; ok {
		panic("storage: Register called twice for adapter " + name)
	}
	adapters[name] = adapter
}

// NewStorage 新的存储
func NewStorage(adapterName, config string) (adapter Storage, err error) {
	adapter, ok := adapters[adapterName]
	if !ok {
		err = fmt.Errorf("storage: unknown adapter name %q (forgot to import?)", adapterName)
		return adapter, err
	}
	return adapter, nil
}
